﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.SystemModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SystemModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;

namespace Resto.CostControl.Core.Facade.SystemModule.FacadeObject
{
    /// <summary>
    /// 字典FO
    /// </summary>
    public class DictionaryFO : IApplicationFacadeObject
    {
        #region 私有属性

        private DictionaryDQO _DictionaryDQO;
        private DictionaryDQO DictionaryDQO
        {
            get
            {
                if (_DictionaryDQO == null)
                    _DictionaryDQO = ObjectCreator.Create<DictionaryDQO>();

                return _DictionaryDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddDictionary(DictionaryDTO dicDTO)
        {
            Dictionary dic = dicDTO.ToDictionary();
            dic.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            dicDTO.ID = dic.ID;

            DictionaryVOArrayCache.Remove(dic.TypeCode);
        }

        public virtual void UpdateDictionary(DictionaryDTO dicDTO)
        {
            Dictionary dic = DictionaryDQO.GetDictionary(dicDTO.ID);
            dicDTO.CopyToDictionary(dic);

            RestoCostDomainSession.CurrentSession.SaveChanges();

            DictionaryVOArrayCache.Remove(dic.TypeCode);
        }

        public virtual void DeleteDictionary(string id)
        {
            Dictionary dic = DictionaryDQO.GetDictionary(id);

            dic.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            DictionaryVOArrayCache.Remove(dic.TypeCode);
        }

        public virtual DictionaryDTO GetDictionaryDTO(string id)
        {
            Dictionary supplier = DictionaryDQO.GetDictionary(id);
            return new DictionaryDTO(supplier);
        }

        #endregion
    }
}
